package cn.tedu.kcircle.attachment.adapter.controller;

import cn.tedu.kcircle.attachment.adapter.converter.AttachmentAdapterConverter;
import cn.tedu.kcircle.attachment.protocol.param.UploadRequestParam;
import cn.tedu.kcircle.attachment.service.AttachmentService;
import cn.tedu.kcircle.attachment.service.bo.UploadRequestBO;
import cn.tedu.kcircle.attachment.service.bo.UploadResultBO;
import cn.tedu.kcircle.common.enums.ResourceType;
import cn.tedu.kcircle.common.po.CurrentPrincipal;
import cn.tedu.kcircle.common.restful.JsonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@Tag(name = "上传中心")
@AllArgsConstructor
@Slf4j
public class AttachmentController {
    private AttachmentService attachmentService;
    private AttachmentAdapterConverter converter;
    @PostMapping("upload/question/image")
    @Operation(summary = "上传问题图片描述")
    public JsonResult<UploadResultBO> uploadQuestionImage(@AuthenticationPrincipal CurrentPrincipal principal, UploadRequestParam uploadRequestParam) throws IOException {
        log.info("接收到上传文件请求 参数为:{}", uploadRequestParam);
        UploadRequestBO uploadRequestBO = converter.param2BO(uploadRequestParam, ResourceType.RESOURCE_TYPE_QUESTION.getType(), principal);
        UploadResultBO uploadResultBO = attachmentService.uploadQuestionImage(uploadRequestBO);
        return JsonResult.ok(uploadResultBO);
    }

    @PostMapping("upload/question/video")
    @Operation(summary = "上传问题视频描述")
    public JsonResult<UploadResultBO> upload(@AuthenticationPrincipal CurrentPrincipal principal,UploadRequestParam uploadRequestParam) {
        log.warn("接收到上传文件请求 参数为:{}", uploadRequestParam);
        UploadRequestBO uploadRequestBO = converter.param2BO(uploadRequestParam,ResourceType.RESOURCE_TYPE_QUESTION.getType(), principal);
        UploadResultBO uploadResultBO = attachmentService.uploadQuestionVideo(uploadRequestBO);
        return JsonResult.ok(uploadResultBO);
    }

    @PostMapping("remove")
    @Operation(summary = "删除文件")
    public JsonResult<Void> remove(String... fileList) {
        //TODO 删除文件
        return JsonResult.ok();
    }

}
